﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace WowGuildSite.Armory
{
    public sealed class ArmoryAccessor
    {
        private IArmoryDataSource   dataSource;

        private Hashtable           guildCache      = new Hashtable();
        private Hashtable           characterCache  = new Hashtable();
        private Hashtable           itemCache       = new Hashtable();
        private Hashtable           iconCache       = new Hashtable();
        private IDictionary         guildRanks;

        internal IDictionary        GuildRanks
        {
            get { return guildRanks; }
        }

        public IArmoryDataSource    DataSource
        {
            get { return dataSource; }
        }

        public bool                 ExistsGuild(string name)
        {
            return DataSource.GetGuildInfoPageCount(name) > 0;
        }

        public ArmoryGuild          GetGuild(string name)
        {
            if (!guildCache.Contains(name))
            {
                guildCache[name] = new ArmoryGuild(this, name);
            }

            return guildCache[name] as ArmoryGuild;
        }

        public bool                 ExistsCharacter(string name)
        {
            return !string.IsNullOrEmpty(DataSource.GetCharacterSheetData(name));
        }

        public ArmoryCharacter      GetCharacter(string name)
        {
            if (!characterCache.Contains(name))
            {
                characterCache[name] = new ArmoryCharacter(this, name);
            }

            return characterCache[name] as ArmoryCharacter;
        }

        public bool                 ExistsItem(ArmoryItemId id)
        {
            return !string.IsNullOrEmpty(DataSource.GetItemInfoData(id.MainId));
        }

        public ArmoryItem           GetItem(ArmoryItemId id)
        {
            if (!itemCache.Contains(id))
            {
                itemCache[id] = new ArmoryItem(this, id);
            }

            return itemCache[id] as ArmoryItem;
        }

        public bool                 HasItemRandomProperties(ArmoryItemId id)
        {
            string content = DataSource.GetItemInfoData(id.MainId);
            if (!string.IsNullOrEmpty(content))
            {
                return content.Contains("randomProperty");
            }
            return false;
        }

        public bool                 ExistsIcon(IconType type,object id )
        {
            return DataSource.GetIconData(type, id).Count > 0;
        }

        public ArmoryIcon           GetIcon(IconType type, object id)
        {
            if (!iconCache.Contains(id))
            {
                iconCache[id] = new ArmoryIcon(this, type, id);
            }
            return iconCache[id] as ArmoryIcon;
        }

        public ArmoryAccessor( IArmoryDataSource dataSource,IDictionary guildRanks )
        {
            if ( dataSource == null )
            {
                throw new ArgumentNullException("dataSource");
            }

            this.dataSource = dataSource;
            this.guildRanks = guildRanks;
            if ( this.guildRanks == null )
            {
                this.guildRanks = new Hashtable();
            }
        }
    }
}
